﻿using Sino.CommonServices.EventTypes;
using Sino.CommonServices.MessageTypes;
using Sino.TMSystem;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.WebApi.Utilities
{

    public static class EventDataTypeMappings
    {

        public static Assembly[] EventDataAssemblies;
        public static ConcurrentDictionary<string, Type> Mappings;

        static EventDataTypeMappings()
        {
            EventDataAssemblies = new Assembly[]
            {
                typeof(UserDetails).Assembly,
                typeof(CarAddEventData).Assembly,
                typeof(GrabbedOrderEventData).Assembly,
                typeof(Sino.PaymentsSystem.Event.PaymentResultEventData).Assembly,
            };
            Mappings = new ConcurrentDictionary<string, Type>();
        }

        public static Type GetType(string typeName)
        {
            if (Mappings.ContainsKey(typeName))
            {
                return Mappings[typeName];
            }
            Type result = null;
            foreach (var asm in EventDataAssemblies)
            {
                var type = asm.GetType(typeName);
                if (type != null)
                {
                    result = type;
                    break;
                }
            }
            Mappings[typeName] = result;
            return result;
        }

    }

}
